package com.cyf.pay.alipay;

import com.alipay.api.internal.util.AlipaySignature;
import com.cyf.pay.alipay.service.AlipayService;
import com.cyf.util.AlipayConfig;
import com.cyf.util.SignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 接收微信的请求 https
 */
@RestController
@RequestMapping(value = "alipay")
@ApiIgnore	// 不再swagger中显示
public class AlipayHttpController {

	private static final Logger logger = LoggerFactory.getLogger(AlipayHttpController.class);

	@Autowired
	private AlipayService alipayService;

	@PostMapping(value = "/payNotify")
	public String payNotify(HttpServletRequest request){
		logger.info("接收到支付宝异步通知===============");
		try {
			Map<String, String> params = SignUtil.request2Map(request);
			boolean flag = AlipaySignature.rsaCheckV1(params, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGN_TYPE);
			if (!flag) {
				logger.info("验签失败！");
				return "fail";
			}
			return alipayService.payNotify(params);
		} catch (Exception e) {
			logger.error("处理APP支付宝支付回调异常：{}", e);
			return AlipayConfig.NOTIFY_RETURN_FAIL;
		}
	}

}
